﻿using DataBaseToExcel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using VDTS_Client.Model;

namespace VDTS_Client
{
    public partial class Form1 : Form
    {
        public static string xmlPath = Application.StartupPath + "\\test.xml";
        DataTable dt;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {



        }

        public static bool IsOdd(int n)
        {
            return Convert.ToBoolean(n % 2);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            //time1=dateTimePicker1
            //localhost.SqlServices sqls = new localhost.SqlServices();
            //DataTable dt1 = sqls.GetAllData("20160918", "20160921");
            ServiceReference1.SqlServicesSoapClient s = new ServiceReference1.SqlServicesSoapClient();

            DataTable dt1 = s.GetAllData("2016-09-28", "2016-09-29");

            decimal sjzyl;//时间占有率
            decimal ctsj;//车头时距
            decimal cljj;//车辆间距

            decimal ct2 = 0;//车头2
            decimal ct1 = 0;//车头1
            decimal cw2 = 0;//车尾2
            decimal cw1 = 0;//车尾1


            ////测试项目 精度要求    分辨率
            ////车流量      99 % 1
            ////瞬时车速    99 % 0.1km / h
            ////时间占有率   98 % 0.1 %
            ////车辆长度    90 % 0.1m
            ////车头时距    90 % 0.1s
            ////    车型    90 % 1


            dt = new DataTable("fcstXML"); //建立一张叫做fcstXML的表
                                           //创建列
            //dt.Columns.Add(new DataColumn("车辆编号", typeof(string)));
            //dt.Columns.Add(new DataColumn("过车时间", typeof(string)));
            dt.Columns.Add(new DataColumn("时间占有率", typeof(string)));
            dt.Columns.Add(new DataColumn("瞬时车速", typeof(string)));
            dt.Columns.Add(new DataColumn("车辆长度", typeof(string)));
            dt.Columns.Add(new DataColumn("车辆类型", typeof(string)));
            dt.Columns.Add(new DataColumn("车头时距", typeof(string)));


            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                DataRow row = dt.NewRow();

                int bid = (int)dt1.Rows[i]["id"];
                int cdh = (int)dt1.Rows[i]["cdh"];
                decimal cwhm = (decimal)dt1.Rows[i]["cwhm"];
                decimal cthm = (decimal)dt1.Rows[i]["cthm"];
                int clbh = (int)dt1.Rows[i]["clbh"];
                int clsd = (int)dt1.Rows[i]["clsd"];
                int clcd = (int)dt1.Rows[i]["clcd"];
                int cllx = (int)dt1.Rows[i]["cllx"];
                DateTime gcsj = (DateTime)dt1.Rows[i]["gcsj"];

                sjzyl = Math.Abs((cwhm - cthm) / 1234);
                bool isodd = IsOdd(i);
                if (isodd)
                {
                    ct2 = cthm;
                    cw2 = cwhm;
                }
                else
                {
                    ct1 = cthm;
                    cw1 = cwhm;
                }
                if (ct2 == 0)
                {
                    ctsj = 0;
                    cljj = 0;
                }
                else
                {
                    ctsj = Math.Abs(ct2 - ct1);
                    cljj = Math.Abs(ct2 - cw1);
                }

                row["时间占有率"] = sjzyl;
                row["瞬时车速"] = clsd;
                row["车辆长度"] = clcd;
                row["车辆类型"] = cllx;
                row["车头时距"] = ctsj;

                dt.Rows.Add(row);

            }








            /*///导入xml文档
            XElement doc = XElement.Load(xmlPath);
            //string deviceNumber = XMLProcess.Read(Application.StartupPath + "\\JCBK.xml", "/JCBKCS/DeviceNumber");
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(xmlPath);
            XmlNode xn = xmldoc.SelectSingleNode("Data");
            //int nodesCount = xn.ChildNodes.Count - 9;

            //for (int i = 1; i <= xn.ChildNodes.Count; i++)
            //{
                //string xh = Convert.ToString(i);
            string deviceXH = "DataContent";
            var node = from n in doc.Elements(deviceXH) select n;
            ///遍历<centerInfo>的结点
            foreach (var xnode in node)
            {   ///创建一个新行
                DataRow row = dt.NewRow();
                ///读取结点数据，并填充数据行
                string cwhm = xnode.Element("cwhm").Value;
                string cthm = xnode.Element("cthm").Value;
                ct2 = Convert.ToDouble(cthm);
                sjzyl = Convert.ToDouble(cwhm) - Convert.ToDouble(cthm);

                if (ct1!=0)
                {
                    ctsj = ct2 - ct1;
                }
                row["过车时间"] = xnode.Element("time").Value; ;
                row["车辆编号"] = xnode.Element("clbh").Value; ;
                row["车头毫秒数"] = xnode.Element("cthm").Value;
                row["车辆速度"] = xnode.Element("clsd").Value;
                row["车辆长度"] = xnode.Element("clcd").Value;
                row["车辆类型"] = xnode.Element("cllx").Value;
                row["车道号"] = xnode.Element("cdh").Value;
                row["车位毫秒"] = xnode.Element("cwhm").Value;

                dt.Rows.Add(row);
                ct1 = Convert.ToDouble(cthm);

            }*/



            //}
            ///创建DataSet对象ds
            DataSet ds = new DataSet("dfdf");
            ds.Tables.Add(dt);
            dataGridView1.DataSource = ds.Tables[0];

/*
            //设置图表的数据源
            chart1.DataSource = dt;

            //设置图表Y轴对应项
            chart1.Series[0].YValueMembers = "时间";
            //chart1.Series[1].YValueMembers = "Volume2";

            //设置图表X轴对应项
            chart1.Series[0].XValueMember = "车辆编号";

            //绑定数据
            chart1.DataBind();*/

            if (radioButton1.Checked)
            {

            }
            else if (radioButton2.Checked)
            {

            }
            else if (radioButton3.Checked)
            {

            }
        }
        /// <summary>
        /// 导出excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            this.saveFileDialog1.FileName = DateTime.Now.ToString("yyyy-M-dd-H-mm-ss");
            this.saveFileDialog1.Filter = "Excel Files(*.xls)|*.xls|Xml Files(*.xml)|*.xml|Txet Files(*.txt)|*.txt";
            if (DialogResult.OK == this.saveFileDialog1.ShowDialog())
            {
                ExcelHelper.ExportDTtoExcel(dt, "查询信息", this.saveFileDialog1.FileName);
/*
                ExpDatagridview tool = new ExpDatagridview(dgv_road, this.saveFileDialog1.FileName);
                if (tool.Exp() == true)
                {
                    tool.Close();
                    //oper_rz.Czjg = Publicclass.Log.log_czjg.success;
                    try
                    {
                        int xx = pp.CW.Bs.InsertNewLog(oper_rz);
                        if (xx != 0)
                        {
                            //oper_rz.Czjg = Publicclass.Log.log_czjg.failure;
                            //pp.CW.Bs.InsertNewLog(oper_rz);
                            //BulidWcf.log.Error("->roadmgr_UI->ExpData->InsertLog->Failure");
                        }
                    }
                    catch (System.Exception ex)
                    {
                        //oper_rz.Czjg = Publicclass.Log.log_czjg.failure;
                        //pp.CW.Bs.InsertNewLog(oper_rz);
                        //BulidWcf.log.Error("->roadmgr_UI->ExpData->InsertLog->Failure", ex);
                    }
                    //导出成功
                    //gtt//MessageBox.Show(Resource.GetString("info_expsuccess"), Resource.GetString("info_notice"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //JariMsgBox.Show(Properties.Resources.info_expsuccess, Properties.Resources.info_notice, JariMsgBox.JariButtons.OK, JariMsgBox.JariIcon.Information);
                }
                else
                {
                    tool.Close();
                    //oper_rz.Czjg = Publicclass.Log.log_czjg.failure;
                    //pp.CW.Bs.InsertNewLog(oper_rz);
                    ////导出失败
                    ////gtt//MessageBox.Show(Resource.GetString("info_expfailure"), Resource.GetString("info_notice"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //JariMsgBox.Show(Properties.Resources.info_expfailure, Properties.Resources.info_notice, JariMsgBox.JariButtons.OK, JariMsgBox.JariIcon.Information);

                }*/
            }
        }
    }
}
